package org.mozhu.mboot.admin.system.auth.entity;

import com.google.common.collect.Sets;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.mozhu.mboot.core.data.entity.BaseEntity;
import org.mozhu.mboot.core.data.hibernate.type.CollectionToStringType;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import java.util.Set;

@TypeDef(name = "SetToStringUserType", typeClass = CollectionToStringType.class, parameters = {@Parameter(name = "separator", value = ","),
        @Parameter(name = "collectionType", value = "java.util.HashSet"), @Parameter(name = "elementType", value = "java.lang.Long")})
@MappedSuperclass
@Data
@EqualsAndHashCode(callSuper = true)
public abstract class AuthSupport extends BaseEntity {

    @Type(type = "SetToStringUserType")
    @Column(name = "role_ids")
    private Set<Long> roleIds;

    @Type(type = "SetToStringUserType")
    @Column(name = "permission_ids")
    private Set<Long> permissionIds;

    public Set<Long> getRoleIds() {
        if (roleIds == null) {
            roleIds = Sets.newHashSet();
        }
        return roleIds;
    }
    
    public Set<Long> getPermissionIds() {
        if (permissionIds == null) {
            permissionIds = Sets.newHashSet();
        }
        return permissionIds;
    }

    public void addRoleIds(Set<Long> roleIds) {
        getRoleIds().addAll(roleIds);
    }

    public void addPermissionIds(Set<Long> permissionIds) {
        getPermissionIds().addAll(permissionIds);
    }
}
